package com.isunland.managesystem.ui;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.NavUtils;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import com.isunland.managesystem.R;
import com.isunland.managesystem.base.BaseFragment;
import com.isunland.managesystem.base.BaseNetworkDialogFragment;
import com.isunland.managesystem.base.BaseTimeDialogFragment;
import com.isunland.managesystem.base.BaseYMDTimeDialogFragment;
import com.isunland.managesystem.entity.CustomerDialog;
import com.isunland.managesystem.entity.ExtraValueContent;
import com.isunland.managesystem.utils.MyDateUtil;
import java.util.Date;

/**
 * 对象功能:
 * 开发公司: 北京信普达系统工程有限公司
 * 开发人员: HuXiaoDan
 * 创建时间: 2017/6/28 10:22
 */
public class StockMaterialQueryFragment extends BaseFragment {
  public static final String EXTRA_VALUE = BaseFragment.class.getSimpleName() + ".EXTRA_VALUE";
  private static final int START_DATE = 0;
  private static final int END_DATE = 1;
  private static final int WAREHOUSE = 2;

  @BindView(R.id.tv_warehouse) TextView tvWarehouse;
  @BindView(R.id.ib_warehouse) ImageView ibWarehouse;
  @BindView(R.id.tv_start_date) TextView tvStartDate;
  @BindView(R.id.ib_start_date) ImageView ibStartDate;
  @BindView(R.id.tv_end_date) TextView tvEndDate;
  @BindView(R.id.ib_end_date) ImageView ibEndDate;

  private String warehouseId = "";
  private ExtraValueContent queryContent;

  @Override public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setBack(true);
    setTitleCustom(R.string.topicQuery);
    queryContent = (ExtraValueContent) getArguments().getSerializable(EXTRA_VALUE);
    warehouseId = queryContent.getExtCode1();
  }

  @Override public View onCreateView(LayoutInflater inflater, ViewGroup container,
      Bundle savedInstanceState) {
    View rootView = super.onCreateView(inflater, container, savedInstanceState);
    ButterKnife.bind(this, rootView);
    tvWarehouse.setText(queryContent.getExtName1());
    tvStartDate.setText(queryContent.getExtName2());
    tvEndDate.setText(queryContent.getExtName3());
    return rootView;
  }

  @Override public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    inflater.inflate(R.menu.menu_confirm, menu);
    super.onCreateOptionsMenu(menu, inflater);
  }

  @Override protected int initLayoutId() {
    return R.layout.fragmennt_stock_search;
  }

  @Override public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
      case android.R.id.home:
        if (NavUtils.getParentActivityName(getActivity()) != null) {
          NavUtils.navigateUpFromSameTask(getActivity());
        }
        break;
      //查询
      case R.id.menu_item_confirm:
        volleyPoastSearch();
        break;

      default:
        break;
    }
    return super.onOptionsItemSelected(item);
  }

  //按条件查询订单
  private void volleyPoastSearch() {
    queryContent = new ExtraValueContent();
    queryContent.setExtName1(tvWarehouse.getText().toString());
    queryContent.setExtCode1(warehouseId);
    queryContent.setExtName2(tvStartDate.getText().toString());
    queryContent.setExtName3(tvEndDate.getText().toString());
    Intent i = new Intent();
    i.putExtra(EXTRA_VALUE, queryContent);
    getActivity().setResult(Activity.RESULT_OK, i);
    getActivity().finish();
  }

  @OnClick({ R.id.ib_warehouse, R.id.ib_start_date, R.id.ib_end_date })
  public void onClick(View view) {
    switch (view.getId()) {
      case R.id.ib_warehouse:
        showDialog(new WareHouseDialogFragment(), WAREHOUSE);
        break;
      case R.id.ib_start_date:
        showDialog(new BaseYMDTimeDialogFragment(), START_DATE);
        break;
      case R.id.ib_end_date:
        showDialog(new BaseYMDTimeDialogFragment(), END_DATE);
        break;
      default:
        break;
    }
  }

  @Override public void onActivityResult(int requestCode, int resultCode, Intent data) {

    if (resultCode != Activity.RESULT_OK) {
      return;
    }
    if (requestCode == START_DATE) {
      Date dateate = (Date) data.getSerializableExtra(BaseTimeDialogFragment.EXTRA_DATE);
      String startDate = MyDateUtil.dateToString(dateate, MyDateUtil.TYPE_DATE);
      tvStartDate.setText(startDate);
      return;
    }
    if (requestCode == END_DATE) {
      Date date = (Date) data.getSerializableExtra(BaseTimeDialogFragment.EXTRA_DATE);
      String endDate = MyDateUtil.dateToString(date, MyDateUtil.TYPE_DATE);
      tvEndDate.setText(endDate);
      return;
    }
    if (requestCode == WAREHOUSE) {
      CustomerDialog customer =
          (CustomerDialog) data.getSerializableExtra(BaseNetworkDialogFragment.EXTRA_VALUE);
      String warehouseName = customer.getName();
      warehouseId = customer.getId();
      tvWarehouse.setText(warehouseName);
    }
    super.onActivityResult(requestCode, resultCode, data);
  }

  public static StockMaterialQueryFragment newInstance(ExtraValueContent queryContent) {

    Bundle args = new Bundle();
    args.putSerializable(EXTRA_VALUE, queryContent);
    StockMaterialQueryFragment fragment = new StockMaterialQueryFragment();
    fragment.setArguments(args);
    return fragment;
  }
}
